package com.happle.gcmclient.utility;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.util.Log;

import com.happle.gcmclient.R;
import com.happle.gcmclient.config.Constants;
import com.happle.gcmclient.obj.LngObj;

/**
 * @Project : GCM Client
 * @File : XMLParserUtility.java
 * @Author : Dang LV
 * @Created : 27-10-2013 12:34:34
 * @Description :
 * 
 @Copyright (C) danglv.hut@gmailcom 2013 . All rights reserved.
 */
public class XMLParserUtility {

	private Context context;

	public XMLParserUtility(Context context) {
		this.context = context;
	}

	public ArrayList<LngObj> getLangugeList(Context context) {
		ArrayList<LngObj> lsvLanguge = new ArrayList<LngObj>();
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			try {
				doc = db.parse(context.getResources()
						.openRawResource(R.raw.lng));
			} catch (NotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (doc != null) {
			doc.getDocumentElement().normalize();

			// Get list node by root tag :
			NodeList nodeList = doc.getElementsByTagName("lng");
			LngObj languge;
			Element element;
			Log.d("ListLng", "Size: " + nodeList.getLength());
			for (int i = 0; i < nodeList.getLength(); i++) {
				element = (Element) nodeList.item(i);
				languge = new LngObj();
				languge.set_lng_id(getValue(element, Constants.lng_id_p));
				languge.set_lng_name(getValue(element, Constants.lng_name_p));
				String isMarked = getValue(element, Constants.is_marked_p);
				languge.set_is_marked(isMarked.equalsIgnoreCase("1") ? true
						: false);
				String isSent = getValue(element, Constants.is_sent_p);
				languge.set_is_sent(isSent.equalsIgnoreCase("1") ? true : false);
				Log.d("Language",
						languge.get_lng_id() + " :" + languge.get_lng_name()
								+ ": " + languge.is_is_marked());
				lsvLanguge.add(languge);
			}
		}
		return lsvLanguge;
	}

	public String getValue(Element item, String str) {
		NodeList n = item.getElementsByTagName(str);
		return this.getElementValue(n.item(0));
	}

	public final String getElementValue(Node elem) {
		Node child;
		if (elem != null) {
			if (elem.hasChildNodes()) {
				for (child = elem.getFirstChild(); child != null; child = child
						.getNextSibling()) {
					if (child.getNodeType() == Node.TEXT_NODE) {
						return child.getNodeValue();
					}
				}
			}
		}
		return "";
	}

}
